package com.queclink.ycm.common.qms;

import java.io.UnsupportedEncodingException;
import java.text.DecimalFormat;

/**
 * describe TODO
 *
 * @author carr.wang
 * @date 2019/2/25 11:00
 */
public class HexUtils {

    /**
     * hex 转String
     *
     * @param hexString
     * @return
     */
    public static String fromHexString(String hexString) {
        String result = "";
        hexString = hexString.toUpperCase();
        String hexDigital = "0123456789ABCDEF";
        //将16进制转化成char数组
        char[] hexs = hexString.toCharArray();

        byte[] bytes = new byte[hexString.length() / 2];

        int n;
        for (int i = 0; i < bytes.length; i++) {
            n = hexDigital.indexOf(hexs[2 * i]) * 16 + hexDigital.indexOf(hexs[2 * i + 1]);
            bytes[i] = (byte) (n & 0xff);
        }

        try {
            result = new String(bytes, "UTF-8");
        } catch (UnsupportedEncodingException ex) {

        }

        return result;
    }
    /**
     * 16进制转10进制
     *
     * @param hex
     * @return
     */
    public static String hex2decimal(String hex) {
        byte[] bytes = new byte[hex.length() / 2];
        String n = "";
        for (int i = 0; i < bytes.length; i++) {
            int parseInt = Integer.parseInt(hex.substring(i * 2, (i + 1) * 2), 16);
            if (i == bytes.length - 1) {
                n = n + parseInt + "";
            } else {
                n = n + String.format("%2d", parseInt).replace(" ", "0");
            }
        }
        return n;
    }

    public static String getBufHexStr(byte[] raw) {
        String HEXES = "0123456789ABCDEF";
        if (raw == null) {
            return null;
        }
        final StringBuilder hex = new StringBuilder(2 * raw.length);
        for (final byte b : raw) {
            hex.append(HEXES.charAt((b & 0xF0) >> 4))
                    .append(HEXES.charAt((b & 0x0F)));
        }
        return hex.toString();
    }

    /**
     * 解析出时间
     *
     * @param hex
     * @return
     */
    public static String hexParseUpdateTime(String hex) {
        DecimalFormat df1 = new DecimalFormat("0000");
        DecimalFormat df2 = new DecimalFormat("00");
       // System.out.println(""+hex);
        int dat0 = (Integer.parseInt(hex.substring(0, 2), 16));
        int dat1 = (Integer.parseInt(hex.substring(2, 4), 16));
        int dat2 = (Integer.parseInt(hex.substring(4, 6), 16));
        int dat3 = (Integer.parseInt(hex.substring(6, 8), 16));
        int dat4 = (Integer.parseInt(hex.substring(8, 10), 16));
        int Year = (dat0 << 6) + (dat1 >> 2);//14 bit
        int Month = ((dat1 & 0x03) << 2) + ((dat2) >> 6);//4 bit
        int Day = (dat2 & 0x3E) >> 1;//5 bit 
        int Hour = ((dat2 & 0x01) << 4) + ((dat3) >> 4);//5 bit
        int Minute = ((dat3 & 0x0F) << 2) + ((dat4) >> 6);//6 bit
        int Second = (dat4 & 0x3F);//6 bit
        
        String result;
        result = String.format(df1.format(Year) + df2.format(Month) + df2.format(Day) + df2.format(Hour) + df2.format(Minute) + df2.format(Second));
        return result;
    }   
    /**
     * 16进制转化为2进制字符串
     *
     * @param hexString
     * @return
     */
    public static String hexString2binaryString(String hexString) {
        if (hexString == null || hexString.length() % 2 != 0) {
            return null;
        }
        String bString = "", tmp;
        for (int i = 0; i < hexString.length(); i++) {
            tmp = "0000" + Integer.toBinaryString(Integer.parseInt(hexString.substring(i, i + 1), 16));
            bString += tmp.substring(tmp.length() - 4);
        }
        return bString;
    }   
}
